package com.hskn.hss.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.DateParser;
import cn.hutool.core.util.StrUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static cn.hutool.core.date.DateUtil.parse;

public class DateUtils {

    /**
     * 天数差
     *
     * @param fromDate
     * @param toDate
     * @return
     * @throws ParseException
     */
    public static Integer calculateDayDifference(Date fromDate, Date toDate) throws ParseException {
        long fromDateTime = fromDate.getTime();
        long toDateTime = toDate.getTime();
        int days = (int) ((toDateTime - fromDateTime) / (1000 * 60 * 60 * 24));
        return days;
    }

    /**
     * 小时差
     *
     * @param fromDate
     * @param toDate
     * @return
     * @throws ParseException
     */
    public static Integer calculateHourDifference(Date fromDate, Date toDate) throws ParseException {
        long fromDateTime = fromDate.getTime();
        long toDateTime = toDate.getTime();
        int hours = (int) ((toDateTime - fromDateTime) / (1000 * 60 * 60));
        return hours;
    }

    /**
     * 分钟差
     *
     * @param fromDate
     * @param toDate
     * @return
     * @throws ParseException
     */
    public static Integer calculateMinuteDifference(Date fromDate, Date toDate) throws ParseException {
        long fromDateTime = fromDate.getTime();
        long toDateTime = toDate.getTime();
        int minutes = (int) ((toDateTime - fromDateTime) / (1000 * 60));
        return minutes;
    }

    /**
     * 秒差
     *
     * @param fromDate
     * @param toDate
     * @return
     * @throws ParseException
     */
    public static Long calculateSecondDifference(Date fromDate, Date toDate) throws ParseException {
        long between = toDate.getTime() - fromDate.getTime();
        long day = between / (24 * 60 * 60 * 1000);
        long hour = (between / (60 * 60 * 1000) - day * 24);
        long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long second = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        return second;
    }


    public static String getCurrentSeason(Date now) {
        String season = "winter";
        Integer year = Calendar.getInstance().get(Calendar.YEAR);
        boolean isSummer = DateUtil.isIn(now, DateUtil.parseDate(year + "-05-01"), DateUtil.parseDate(year + "-10-01"));
        if (isSummer) {
            season = "summer";
        }
        return season;
    }

    public static DateTime parseTimeDate(Date date, CharSequence timeString) {
        String dateTime = DateUtil.formatDate(date);
        timeString = StrUtil.format("{} {}", new Object[]{dateTime, timeString});
        return 1 == StrUtil.count(timeString, ':') ? parse((CharSequence) timeString, (String) "yyyy-MM-dd HH:mm") : parse((CharSequence) timeString, (DateParser) DatePattern.NORM_DATETIME_FORMAT);
    }


    /**
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @author sunran   判断当前时间在时间区间内
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }


    public static boolean checkDateTime() throws ParseException {
        Date date1 = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(date1);
        c.add(Calendar.DAY_OF_MONTH, 1);
        date1 = c.getTime();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        String calendarDate = sdf1.format(date1);

        String date = StringUtils.dateToStr(new Date(), "yyyy-MM-dd");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time1 = date + " 23:00:00";

        String time2 = calendarDate + " 01:00:00";

        System.out.println(time1);
        System.out.println(time2);
        Date startTime = format.parse(time1);
        Date endTime = format.parse(time2);

        Date nowTime = new Date();
//        String nowTime1 = "2021-12-05 01:00:00";
//        Date parse = format.parse(nowTime1);

        boolean effectiveDate = isEffectiveDate(nowTime, startTime, endTime);
        System.out.println(startTime);
        System.out.println(endTime);
        if (effectiveDate) {
            System.out.println("当前时间在范围内");
            return false;
        } else {
            System.out.println("当前时间在不在范围内");
            return true;
        }

    }

    /**
     * 用于设定现在时间提前多少小时的
     * @param startDate:开始时间 ，yyyy-MM-dd HH:mm:ss
     * @param  hours :增加的时间 （提前，用正整数， 推后时间用负整数）
     */
    public static String getPostponeTimesOradvanceTimesByHour(Date startDate, int hours){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String startTime = sdf.format(startDate);
        System.out.println("现在时间："+startTime);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hours);
        String endTime = sdf.format(calendar.getTime());
        System.out.println("提前"+hours+"小时："+ endTime);
        return endTime;
    }

    /**
     * 用于设定现在时间提前多少分钟的
     * @param startDate:开始时间 yyyy-MM-dd HH:mm:ss
     * @param  minute :增加的时间 （提前，用正整数， 推后时间用负整数）
     */
    public static Date getpostponetimesoradvancetimesByMinute(Date startDate, int minute){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - minute);
        String endTime = sdf.format(calendar.getTime());
        return DateUtil.parseDateTime(endTime);
    }

}
